package com.wangsd.web.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.sys.entity.SysMenu;
import com.wangsd.web.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-02
 */
@Api(value = "sysRole", tags = "菜单管理")
@RestController
@RequestMapping("/sys/sysMenu")
public class SysMenuController {

    @Autowired
    SysMenuService sysMenuService;

    @ApiOperation(value = "查询所有一级菜单", notes = "查询所有一级菜单", httpMethod = "GET")
    @GetMapping("/selectByNull")
    public Result selectByNull() {

        List<SysMenu> list = sysMenuService.selectByNull();
        return Result.success(list);

    }

    @ApiOperation(value = "查询菜单列表", notes = "查询菜单列表", httpMethod = "GET")
    @GetMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@ApiParam(value = "当前页", required = true) @PathVariable Integer pageNum,
                             @ApiParam(value = "每页大小", required = true) @PathVariable Integer pageSize) {

        IPage<SysMenu> sysUserIPage = sysMenuService.selectList(pageNum, pageSize);
        return Result.success(sysUserIPage);

    }

    @ApiOperation(value = "按层级查询所有菜单（配置权限）", notes = "按层级查询所有菜单（配置权限）", httpMethod = "GET")
    @GetMapping("/selectAll")
    public Result selectAll() {

        List<SysMenu> list = sysMenuService.selectAll();
        return Result.success(list);

    }

    @ApiOperation(value = "删除菜单", notes = "删除菜单", httpMethod = "GET", response = Result.class)
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "菜单id", required = true) @PathVariable Long id) {

        int delete = sysMenuService.delete(id);
        return Result.success(delete);

    }

    @ApiOperation(value = "新增或修改菜单信息", notes = "新增或修改菜单信息", httpMethod = "POST", response = Result.class)
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody SysMenu sysMenu) {

        int i = sysMenuService.addOrUpdate(sysMenu);
        return Result.success(i);

    }

    @ApiOperation(value = "按字段更新角色（直接更新排序字段）", notes = "按字段更新角色（直接更新排序字段）", httpMethod = "POST", response = Result.class)
    @PostMapping("/updateOther")
    public Result updateOther(@ApiParam(value = "id", required = true) @RequestParam("id") Long id,
                              @ApiParam(value = "名称", required = true) @RequestParam("name") String name,
                              @ApiParam(value = "value", required = true) @RequestParam("value") String value) {

        if (id == null || StringUtils.isBlank(name) || StringUtils.isBlank(value)) {
            return Result.message(PRARM_NOT_NULL);
        }
        int i = sysMenuService.updateOther(id, name, value);
        return Result.success(i);

    }

    @ApiOperation(value = "查询该角色拥有哪些菜单和按钮", notes = "查询该角色拥有哪些菜单和按钮", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectByRoleId/{roleId}")
    public Result selectByRoleId(@ApiParam(value = "菜单id", required = true) @PathVariable Long roleId) {

        List<SysMenu> list = sysMenuService.selectByRoleId(roleId);
        return Result.success(list);

    }

}

